<?php
//获取必传参数
$apiData = $route->bodyParams['apiData'];

//事物执行结果保存
$transactionResult = array();

//关闭事物自动提交
$db->mysqlDB->falseAutoCommit();

//判断接口是否存在,以及参数是否正确
for($j=0;$j<count($apiData);$j++){

    $apiName       = $apiData[$j]['apiName'];
    $apiParamsData = $apiData[$j]['method'] == 'GET' ? $apiData[$j]['params'] : $apiData[$j]['data']; 
    $apiInfo       = $GLOBALS['interfaceData'][$apiName];

    if($apiInfo == NULL){
        $response->responseData(false,"接口:".$apiName." 不存在");
    }

    //接口参数验证，根据接口类型进入不同的验证接口
	if($apiInfo['type'] == 'SYSTEM'){

		//判断接口参数是否满足条件
        $paramsValidation = $GLOBALS['interfaceSystemPrivilege'][$apiName];
        
	}else{

		//判断接口参数是否满足条件
        $paramsValidation = $GLOBALS['interfacePrivilege'][$apiName];
        
    }
    
    //判断是否需要验证		
	if($paramsValidation['getMustParams']!=NULL || $paramsValidation['postMustParams']!=NULL){

        //取出restful必填字段和验证条件
		$restfulFieldArr     = array();
		$restfulConditionArr = array();	
		for($i=0;$i<count($paramsValidation['getMustParams']);$i++){
		   	$tempArr = explode("|",$paramsValidation['getMustParams'][$i]);
		   	array_push($restfulFieldArr,trim($tempArr[0]));
		   	array_push($restfulConditionArr,trim($tempArr[1]));
        }
        
		//取出body必填字段和验证条件
		$bodyFieldArr     = array();
		$bodyConditionArr = array();	

		for($i=0;$i<count($paramsValidation['postMustParams']);$i++){
		   	$tempArr = explode("|",$paramsValidation['postMustParams'][$i]);
		   	array_push($bodyFieldArr,trim($tempArr[0]));
		   	array_push($bodyConditionArr,trim($tempArr[1]));
        }  
        
        $restfulValidation = $restfulFieldArr;
        $bodyValidation    = $bodyFieldArr;
        
        //验证RESTFUL必填参数是否存在
        if($restfulValidation!=NULL){

            //取出当前传递的参数	
            $restfulKeyArr = $apiParamsData==NULL?[]:array_keys($apiParamsData);

            //获取未传递的restful参数
            $noRestFulParams = array_diff($restfulValidation,$restfulKeyArr);

            //如果有未填写的项，返回错误提示
            if(count($noRestFulParams)>0){

                //验证结果提示
                $validationRestFulMessage = implode(",",$noRestFulParams);

                //返回验证结果
                $response->responseData(false,"接口:".$apiName." -> ".$GLOBALS['errorMsg']['restfulParamsError']['message'].":".$validationRestFulMessage);
            
            }
        }

        //restful必填参数进行格式验证
        for($i=0;$i<count($restfulFieldArr);$i++){

            //取出正则验证规则
            $pattern = $GLOBALS['regexpSettings'][$restfulConditionArr[$i]];
            $restfulParamsValue = $apiParamsData[$restfulFieldArr[$i]];
     
            if($pattern){
                if($pattern == 'NOEMPTY'){
                    if( empty($restfulParamsValue) )
                    {
                        $response->responseData(false,"接口:".$apiName." -> "."参数:".$restfulFieldArr[$i]."不能为空");
                    }
                }else{
                    if(!preg_match($pattern, $restfulParamsValue)){
                        $response->responseData(false,"接口:".$apiName." -> "."参数:".$restfulFieldArr[$i]."格式不正确");
                    }
                }
            }else{
                $response->responseData(false,"接口:".$apiName." -> ".$GLOBALS['errorMsg']['noRegexpError']);
            }
        }	


        //验证BODY必填参数是否存在
        if($bodyValidation!=NULL){


            //取出当前存在的body参数
            $bodyKeyArr = $apiParamsData==NULL?[]:array_keys($apiParamsData);
            
            //获取未传递的body参数
            $noBodyParams = array_diff($bodyValidation,$bodyKeyArr);
            
            
            if(count($noBodyParams)>0){

                //验证结果提示
                $validationBodyMessage = implode(",",$noBodyParams);

                //返回验证结果
                $response->responseData(false,"接口:".$apiName." -> ".$GLOBALS['errorMsg']['bodyParamsError']['message'].":".$validationBodyMessage);
            
            }
        }

        //body必填参数进行格式验证
        for($i=0;$i<count($bodyFieldArr);$i++){

            //取出正则验证规则
            $pattern 			= $GLOBALS['regexpSettings'][$bodyConditionArr[$i]];
            $bodyParamsValue    = $apiParamsData[$bodyFieldArr[$i]];
            
     
            if($pattern){
                if($pattern == 'NOEMPTY'){
                    if( empty($bodyParamsValue)){
                        $response->responseData(false,"接口:".$apiName." -> "."参数:".$bodyFieldArr[$i]."不能为空");
                    }
                }else{
         
                    if(!preg_match($pattern, $bodyParamsValue)){
                     $response->responseData(false,"接口:".$apiName." -> "."参数:".$bodyFieldArr[$i]."格式不正确");
                 }
                }
            }else{
                $response->responseData(false,"接口:".$apiName." -> ".$GLOBALS['errorMsg']['noRegexpError']);
            }
        }
    }

    //判断是自动构建还是自定义进行相应的请求操作
    if($apiInfo['autoTable'] != NULL && $apiInfo['autoType'] != NULL){

        //判断构建类型
		if($apiInfo['autoType'] == 'C'){

         
			//新增
			$rsData = $db->mysqlDB->automaticAdd($apiParamsData)->insertTransaction($apiInfo['autoTable']);

		}else if($apiInfo['autoType'] == 'U'){

         
			//更新
			$rsData = $db->mysqlDB->automaticUpdate($apiParamsData)->update($apiInfo['autoTable']);

		}else if($apiInfo['autoType'] == 'R'){

			//查询
			$rsData = $db->mysqlDB->field($apiParamsData)->params($route->restfulParams)->select($apiInfo['autoTable']);

		}else if($apiInfo['autoType'] == 'D'){

          
			//删除
			$rsData = $db->mysqlDB->automaticDelete($apiParamsData)->delete($apiInfo['autoTable']);

		}else{

            $response->responseData(false,$GLOBALS['errorMsg']['autoCURDParamsError']);

        }

        $transactionResult[$j] = array(
            "type" => $apiInfo['autoType'],
            "rs"   => $rsData
        );
  
    }else{

        $response->responseData(false,$GLOBALS['errorMsg']['systemApiTransacationError']);

    }

}

//是否可以进行事物提交
$isCommit = true;

//事物判断
for($i=0;$i<count($transactionResult);$i++){

    if($transactionResult[$i]['type'] == 'C' || $transactionResult[$i]['type'] == 'U'){

        if(!$transactionResult[$i]['rs']){

            $isCommit = false;

            break;

        }
    } 
    
    if($transactionResult[$i]['type'] == 'D'){

        if($transactionResult[$i]['rs']){

            $isCommit = false;

            break;

        }
    }
}


//事物提交
if($isCommit){
    $db->mysqlDB->actionCommint();
    $db->mysqlDB->trueAutoCommit();
    $response->responseData(true);
}else{
    $db->mysqlDB->rollBackCommint();
    $response->responseData(false);
}